home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cmprss.exe / COMPRESS.CPP < prev    next >
C/C++ Source or Header  |  1993-01-26  |  2KB  |  121 lines

  1. #include "compress.cls"
  2.  
  3. cpofstream::cpofstream(void) : idx(0)
  4.     {
  5.     }
  6.  
  7.  
  8. cpofstream::cpofstream(char *fn) : idx(0)
  9.     {
  10.     init();            // allocate space for buffers
  11.     ofstream::open(fn, ios::binary | ios::out);
  12.     }
  13.  
  14.  
  15. cpofstream::~cpofstream()
  16.     {
  17.     flush();
  18.     clear();
  19.     }
  20.  
  21.  
  22. void cpofstream::fillbuf(void* v, int l)
  23.     {
  24.     while (l--)
  25.         {
  26.         inbuf[idx]=*(char*)v;
  27.         if (++idx==CBUF_SIZE)
  28.             encode();
  29.         ((char*)v)++;
  30.         }
  31.     }
  32.  
  33.  
  34. cpofstream& cpofstream::operator<<(char *s)
  35.     {
  36.     while (*s)
  37.         {
  38.         inbuf[idx]=*s;
  39.         if (++idx==CBUF_SIZE)
  40.             encode();
  41.         s++;
  42.         }
  43.     inbuf[idx]=*s;            // have to do NULL also
  44.     if (++idx==CBUF_SIZE)
  45.         encode();
  46.     return *this;
  47.     }
  48.  
  49.  
  50. cpofstream& cpofstream::operator<<(void* v)
  51.     {
  52.     unsigned long l=(unsigned long)v;
  53.     fillbuf(&l,sizeof(unsigned long));
  54.      return *this;
  55.     }
  56.  
  57.  
  58. void cpofstream::open(char *fn)
  59.     {
  60.     init();                    // allocate space for buffers
  61.     ofstream::open(fn, ios::binary | ios::out);
  62.     }
  63.  
  64.  
  65. filebuf* cpofstream::close(void)
  66.     {
  67.     flush();
  68.     clear();                    // deallocate buffers
  69.     return rdbuf()->close();
  70.     }
  71.  
  72.  
  73. cpofstream& cpofstream::flush(void)
  74.     {
  75.     if (idx)
  76.         {
  77.         encode();
  78.         }
  79.     ofstream::write((char*)&idx,sizeof(idx));            // write EO(compressed)F
  80.     reset();
  81.     return *this;
  82.     }
  83.  
  84.  
  85. void cpofstream::encode(void)
  86.     {
  87.     int n=compress(inbuf, idx, outbuf, hash_table, HASH_LEN);
  88.     ofstream::write((char*)&n,sizeof(n));
  89.     if (n<0)
  90.         n=-n;
  91.     ofstream::write(outbuf,n);
  92.     idx=0;
  93.     }
  94.  
  95.  
  96. void cpofstream::init(void)
  97.     {
  98.     inbuf=new unsigned char[CBUF_SIZE];
  99.     outbuf=new unsigned char[CBUF_SIZE];
  100.     hash_table=new unsigned char*[HASH_LEN];
  101.     reset();
  102.     }
  103.  
  104.  
  105. void cpofstream::clear(void)
  106.     {
  107.     delete inbuf;
  108.     delete outbuf;
  109.     delete hash_table;
  110.     }
  111.  
  112.  
  113. void cpofstream::reset(void)
  114.     {
  115.     for (int i=0;i<HASH_LEN;i++)
  116.         {
  117.         hash_table[i]=0L;
  118.         }
  119.     }
  120.  
  121.